package com.botarena.server.servlets.http;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.botarena.server.services.ContestService;

public class CreateContestServlet extends HttpServlet {

	private static final long serialVersionUID = 1343502021179060903L;

	private final ContestService contestService;

	public CreateContestServlet() {
		contestService = new ContestService();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String name = (String)request.getParameter("name");
			String from = (String)request.getParameter("from");
			String to = (String)request.getParameter("to");
			
			System.out.println(name);
			System.out.println(from);
			System.out.println(to);
			
			DateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm");
			Date fromDate = format.parse(from);
			Date toDate = format.parse(to);
			
			contestService.createNewContest(name, fromDate, toDate);
			
		} catch (Exception e) {
			e.printStackTrace();
			String error = "Cannot create account: " + e.getMessage();
			response.getOutputStream().write(error.getBytes());
		} finally {
			response.getOutputStream().close();
		}
	}

}
